<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\api\controller;


use app\api\lists\article\ArticleCollectLists;
use app\api\logic\ArticleLogic;
use app\common\model\article\Supply;
use app\common\model\article\Vip;
use think\response\Json;

/**
 * 文章管理
 * Class ArticleController
 * @package app\api\controller
 */
class SupplyController extends BaseApiController
{

    public array $notNeedLogin = ['lists', 'cate', 'detail'];

    public function viplists()
    {
        return $this->success('操作成功', ['data' => Vip::lists()], 1, 1);
    }

    /**
     * @notes 文章列表
     * @return Json
     * @author 段誉
     * @date 2022/9/20 15:30
     */
    public function lists()
    {
        $name = $this->request->get("name");
        $model = new Supply();
        if (!empty($name)) {
            $model = $model->where('name', 'like', '%' . $name . '%');
        }
 /*       $sevenDaysAgo = time() - 7*24*60*60;*/
        $list = $model->where("start", "=", 1)
    /*        ->where('chuangjiantime', '>=', $sevenDaysAgo)*/
            ->order(['is_top' => 'asc', 'id' => 'desc'])->with(['yonghu'])->select()->toArray();
        return $this->success('操作成功', ['data' => $list], 1, 1);

    }

    /**
     * @notes 文章分类列表
     * @return Json
     * @author 段誉
     * @date 2022/9/20 15:30
     */
    public function cate()
    {
        return $this->data(ArticleLogic::cate());
    }


    /**
     * @notes 收藏列表
     * @return Json
     * @author 段誉
     * @date 2022/9/20 16:31
     */
    public function collect()
    {
        return $this->dataLists(new ArticleCollectLists());
    }

    public function edit()
    {
        $parme = $this->request->post();
        $user = $this->userId;
        $parme['contacts'] = json_encode($parme['contacts']);
        $parme['ycxq']=json_encode($parme['ycxq']);
        $result = (new Supply())->where("id", "=", $parme["id"])->where("user_id", "=", $user)->update($parme);
        return $this->success('操作成功', ['data' => $result], 1, 1);
    }

    public function del()
    {
        $parme = $this->request->post();

        $result = (new Supply())->where("id", "=", $parme["id"])->delete();
        return $this->success('操作成功', ['data' => 1], 1, 1);
    }
    public function details()
    {
        $id = $this->request->get('id');
        $user = $this->userId;

        $supply = new Supply();
        $supply->where("id", "=", $id)->inc('liulan')->update();

        $result = $supply->where("id", "=", $id)->with("yonghu")->findOrEmpty()->toArray();

        return $this->data($result);
    }
    /**
     * @notes 文章详情
     * @return Json
     * @author 段誉
     * @date 2022/9/20 17:09
     */
    public function detail()
    {
        $id = $this->request->get('id');
        $user = $this->userId;
        $result = (new Supply())->where("id", "=", $id)->where("user_id", "=", $user)->findOrEmpty()->toArray();
        return $this->data($result);
    }

    public function supplyadd()
    {
        $parme = $this->request->post();
        $user = $this->userId;
        $parme['user_id'] = $user;
        $parme['contacts'] = json_encode($parme['contacts']);
        $year = date('Y');
        $month = date('m');
        $day = date('d');
        $current_date = $year . '-' . $month . '-' . $day;
        $parme['fbtime'] = $current_date;
        $parme['chuangjiantime']=time();
        $parme['ycxq']=json_encode($parme['ycxq']);
        $model = new Supply();
        $sq = $model->save($parme);
        if ($sq != '') {
            return $this->success('操作成功', ['data' => $sq], 1, 1);
        }
        return $this->success('操作失败', ['data' => 2], 1, 1);
    }

    /**
     * @notes 加入收藏
     * @return Json
     * @author 段誉
     * @date 2022/9/20 17:01
     */
    public function addCollect()
    {
        $articleId = $this->request->post('id/d');
        ArticleLogic::addCollect($articleId, $this->userId);
        return $this->success('操作成功');
    }


    /**
     * @notes 取消收藏
     * @return Json
     * @author 段誉
     * @date 2022/9/20 17:01
     */
    public function cancelCollect()
    {
        $articleId = $this->request->post('id/d');
        ArticleLogic::cancelCollect($articleId, $this->userId);
        return $this->success('操作成功');
    }


}